<template>
    <div>
        <!-- 筛选条件 -->
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
            <el-form-item label="网关" prop="gatewayId">
                <gateway-select @changeGateway="changeGateway"/>
            </el-form-item>
            <el-form-item label="信息点" prop="sensorId">
                <el-select v-model="queryParams.sensorId"
                           size="small"
                           @change="changeSensor"
                           clearable
                >
                    <el-option
                        v-for="item in sensorList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否通知" prop="status">
                <el-select v-model="queryParams.status"
                           size="small"
                           @change="changeStatus"
                           clearable
                >
                    <el-option
                        v-for="item in statusList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否处理" prop="isProcess">
                <el-select v-model="queryParams.isProcess"
                           size="small"
                           @change="changeIsProcess"
                           clearable
                >
                    <el-option
                        v-for="item in isProcessList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    plain
                    :disabled="multiple"
                    @click="processHandler"
                >设为已处理
                </el-button>
<!--                <el-button-->
<!--                    plain-->
<!--                    @click="processHandlerAll"-->
<!--                >一键处理-->
<!--                </el-button>-->
            </el-col>

            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>


        <el-row v-show="dataList.length>0" v-loading="loading" :gutter="20"
                style="margin-top: 10px;font-size: 15px" type="flex" justify="space-around"
                v-for="(item,index) in dataList" :key="index"
        >
            <el-col :xl="24">
                <el-card shadow="always" :class="{ 'selected': isSelected(index) }"
                         @click.native="toggleSelection(index)"
                >
                    <div slot="header" class="clearfix">
                        <span style="font-size: 14px;color: #abb2bf">{{ item.gatewayName }}</span>
                    </div>
                    <div style="display: flex;align-items: center;color: #717783">
                        <span>信息点：</span>
                        <span>{{ item.sensorName }}</span>
                    </div>
                    <div style="display: flex;align-items: center;margin-top: 10px;color: #717783">
                        <span>报警时间：</span>
                        <span>{{ parseTime(item.warnTime) }}</span>
                    </div>

                    <div style="display: flex;align-items: center;margin-top: 10px;color: #717783">
                        <span>是否通知：</span>
                        <div>
                            <el-tag v-if="item.status==='0'" type="info">否</el-tag>
                            <el-tag v-if="item.status==='1'" type="success">是</el-tag>
                        </div>
                    </div>

                    <div style="display: flex;align-items: center;margin-top: 10px;color: #717783">
                        <span>通知时间：</span>
                        <span>{{ parseTime(item.noticeTime) }}</span>
                    </div>

<!--                    <div style="display: flex;align-items: center;margin-top: 10px;color: #717783">-->
<!--                        <span>通知结果：</span>-->
<!--                        <div>-->
<!--                            <el-tag v-if="item.status==='0'" type="info">未通知</el-tag>-->
<!--                            <el-tag v-if="item.status==='1'" type="danger">失败</el-tag>-->
<!--                            <el-tag v-if="item.status==='2'" type="success">部分成功</el-tag>-->
<!--                            <el-tag v-if="item.status==='3'" type="success">全部成功</el-tag>-->
<!--                        </div>-->
<!--                    </div>-->

<!--                    <div style="display: flex;align-items: center;margin-top: 10px;color: #717783">-->
<!--                        <span style="width: 80px">详情：</span>-->
<!--                        <span>{{ item.noticeResult }}</span>-->
<!--                    </div>-->

                    <div style="display: flex;align-items: center;margin-top: 10px;color: #717783">
                        <span>报警值：</span>
                        <span>{{ item.sensorVal }}</span>
                    </div>

                    <div style="display: flex;align-items: center;margin-top: 10px;color: #717783">
                        <span>触发报警规则：</span>
                        <span>{{ item.warnParam }}</span>
                    </div>

                    <div style="display: flex;align-items: center;margin-top: 10px;color: #717783">
                        <span>是否处理：</span>
                        <div>
                            <el-tag v-if="item.isProcess==='0'" type="info">否</el-tag>
                            <el-tag v-if="item.isProcess==='1'" type="success">是</el-tag>
                        </div>
                    </div>

                    <div style="display: flex;align-items: center;margin-top: 10px;color: #717783">
                        <span>处理时间：</span>
                        <span>{{ parseTime(item.processTime) }}</span>
                    </div>

<!--                    <div v-if="item.isProcess==='1'">-->
<!--                        <div style="display: flex;align-items: center;margin-top: 10px;color: #717783">-->
<!--                            <span>处理时间：</span>-->
<!--                            <span>{{ parseTime(item.processTime) }}</span>-->
<!--                        </div>-->
<!--                        <div style="display: flex;align-items: center;margin-top: 10px;color: #717783">-->
<!--                            <span>处理人：</span>-->
<!--                            <span>{{ item.processBy }}</span>-->
<!--                        </div>-->
<!--                    </div>-->
                </el-card>
            </el-col>
        </el-row>

        <el-row v-show="dataList.length<1" :gutter="20" style="margin-top: 10px;" type="flex" justify="space-around">
            <el-col :xl="24">
                <el-empty></el-empty>
            </el-col>
        </el-row>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
    </div>
</template>

<script>
import { pageWarnList, processWarn, processWarnAll } from '@/api/equipment/sensor'
import { listSensorByGatewayId} from '@/api/equipment/sensor'
import gatewaySelect from '@/views/common/GatewaySelect.vue'
// import {processSystemMessageAll} from "@/api/system/message";

export default {
    name: 'index',
    data() {
        return {
            showSearch: true,
            dataList: [],
            total: 0,
            loading: false,
            multiple: true,
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                gatewayId: null,
                status: null,
                isProcess: null
            },
            ids: [],
            selectedItems: [],
            statusList: [
                { 'value': '0', 'label': '未通知' },
                { 'value': '1', 'label': '已通知' }
            ],
            isProcessList: [
                { 'value': '0', 'label': '未处理' },
                { 'value': '1', 'label': '已处理' }
            ],
            sensorList: []
        }
    },
    components: {
        gatewaySelect
    },
    created() {
        this.getList()
    },
    methods: {
        calculateIndex(index) {
            const pageNum = this.queryParams.pageNum
            const pageSize = this.queryParams.pageSize
            return index + 1 + (pageNum - 1) * pageSize
        },
        getList() {
            this.loading = true
            pageWarnList(this.queryParams).then(response => {
                this.dataList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        processHandler() {
            processWarn({ ids: this.ids }).then(response => {
                this.$message.success('成功')
                this.getList()
                this.$emit('getCount')
                this.ids = []
                this.selectedItems = []
            })
        },
        processHandlerAll() {
            processWarnAll().then(response => {
                this.$message.success('成功')
                this.getList()
            })
        },
        // 接收子组件传递的消息
        changeGateway(obj) {
            let gatewayId = obj.gatewayId
            this.queryParams.gatewayId = gatewayId
            this.getList()

            // console.log('gatewayId ', gatewayId)

            this.sensorList = []
            // 点击选择框右边的叉时，传回的值为''，而不是null
            if (gatewayId == null || gatewayId === '') {
                return
            }
            listSensorByGatewayId(gatewayId).then(response => {
                for (let i = 0; i < response.data.length; ++i) {
                    let obj = {}
                    obj['value'] = response.data[i]['sensorId']
                    obj['label'] = response.data[i]['sensorName']
                    this.sensorList.push(obj)
                }
            })
        },
        toggleSelection(itemId) {
            const index = this.selectedItems.indexOf(itemId)
            if (index === -1) {
                this.selectedItems.push(itemId)
                this.ids.push(this.dataList[itemId].id)
            } else {
                this.selectedItems.splice(index, 1)
                this.ids.splice(index, 1)
            }
            this.multiple = !this.selectedItems.length
        },
        isSelected(index) {
            return this.selectedItems.includes(index)
        },
        changeSensor(val) {
            this.getList()
        },
        changeStatus(val) {
            this.getList()
        },
        changeIsProcess(val) {
            this.getList()
        }
    }
}
</script>

<style scoped>
.selected {
    border: 1px solid red;
}
</style>
